import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/utils/ColorUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../base/widget/defult_widget.dart';
import '../../../utils/Utils.dart';
import '../controller/ota_record_controller.dart';
import '../model/moto_record.dart';

class OtaRecordPage extends BaseStatefulWidget<OtaRecordController> {
  @override
  String titleString() => controller.title!;

  @override
  Widget buildContent(BuildContext context) {
    if (controller.showNoData) {
      return noData();
    } else {
      return ListView.builder(
          itemCount: controller.list.length,
          itemBuilder: (context, index) {
            return item(controller.list[index], index, controller.list.length);
          });
    }
  }

  item(MotoRecord? data, int? index, int? length) {
    String? tempName;
    String otaTime = '升级时间：'.tr;
    String desc = '';
    String bug = '升级内容：'.tr;
    switch (data!.status) {
      case 3:
        tempName = '固件升级成功'.tr;
        break;
      default:
        tempName = '固件升级失败'.tr;
        break;
    }

    otaTime = Utils.timeToDate(data.updated_at!);
    for (int i = 0; i < data.detail!.length; i++) {
      if (i < data.detail!.length - 1) {
        if (Utils.isNotEmptyString(data.detail![i].description)) {
          bug = '$bug ${data.detail![i].description},';
        }
      } else {
        if (Utils.isNotEmptyString(data.detail![i].description)) {
          bug = '$bug ${data.detail![i].description}';
        }
      }
      for (int j = 0; j < data.detail![i].type!.length; j++) {
        if (j < data.detail![i].type!.length - 1) {
          if (Utils.isNotEmptyString(data.detail![i].type![j].name)) {
            desc = '$desc ${data.detail![i].type![j].name},';
          }
        } else {
          if (Utils.isNotEmptyString(data.detail![i].type![j].name)) {
            desc = '$desc ${data.detail![i].type![j].name}';
          }
        }
      }
    }
    return Container(
      padding:
          EdgeInsets.only(top: 15.sp, bottom: 15.sp, left: 30.sp, right: 30.sp),
      color: const Color(0xFF434650),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const SizedBox(
            height: 10,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(tempName,
                  style:
                      const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
              Text(otaTime,
                  style:
                      const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
            ],
          ),
          const SizedBox(
            height: 10,
          ),
          Text(desc.trim(),
              style: TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
          const SizedBox(
            height: 10,
          ),
          Text(bug, style: TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
          const SizedBox(
            height: 10,
          ),
          Visibility(
            visible: index! < (length! - 1),
            child: Container(
              color: ColorUtils.color_white,
              height: 1,
            ),
          )
        ],
      ),
    );
  }

  noData() {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      color: const Color(0xFF434650),
      child: Column(children: [
        const SizedBox(
          height: 200,
        ),
        Image.asset('assets/images/home/share/empty_image.png',
            width: 360.sp, height: 298.sp),
        DefaultText(
          '暂无数据'.tr,
          40.sp,
          fontWeight: FontWeight.w700,
          textColor: Colors.white,
        ).marginOnly(top: 32.sp)
      ]),
    );
  }
}
